<?php
namespace App\Http\Controllers\Home;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Components\Tools;
use App\Components\DyToools;
use App\Models\DyModels\Douyin;
use App\Models\DyModels\HandleVideo;

class DyUserMgingController extends Controller{
	//抖音调用接口方法
	private $url='';
	
	function __construct(Request $request){
        $this->parames = $request->all();
		$this->url=Tools::getOneconfig("/config/third.json",array('douyin','dy_url'));
	}
	
	/**
	* 获取用户粉丝、关注列表数据------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	*/
	function getFansList(){
		set_time_limit(0);
		$userTokenList=new HandleVideo();
		$userTokenList=$userTokenList->userTokenList();
	//	echo '<pre/>';print_r($userTokenList);
		if($userTokenList){
			foreach($userTokenList as $k=>$v){
				$this->getFansData($v['open_id'],0);
                $this->getFollowingData($v['open_id'],0);
			}
		}
	}

	//粉丝数据
	function getFansData($open_id,$cursor){
		$userToken=new HandleVideo();
		$infoToken=new Douyin();
        $DyToools=new DyToools();
		$token=$userToken->userToken($open_id);
		$url=$this->url."/fans/list/?open_id={$token['open_id']}&access_token={$token['access_token']}&cursor={$cursor}&count=20";
	//echo '<pre/>';	 print_r($url);
		$data=$this->getJson($url);
		if( isset($data['extra']['error_code']) && $data['extra']['error_code']==0 ){
			if( isset($data['data']['list']) && $data['data']['list'] ){
				//更新、添加用户粉丝数据
				$userToken->upDyUserFans($data['data']['list'],$open_id);
			}
			if( $data['data']['cursor']>0 && $data['data']['has_more'] ){
				$this->getFansData($open_id,$data['data']['cursor']);
			}
			
			
		}elseif( isset($data['extra']['error_code']) && $data['extra']['error_code']==10008 || $data['extra']['error_code']=2190008 ){
			
			
			// access_token 过期
			$dataOne=$DyToools->refresh_token($token['refresh_token']);
			if( isset($dataOne['data']['error_code']) && $dataOne['data']['error_code']==0 ){
				
				
				$infoToken->upToken($dataOne['data'],$open_id);//更新Token
				$this->getFansData($open_id,$cursor);
				
				
			}elseif( isset($dataOne['data']['error_code']) && $dataOne['data']['error_code']==10010 ){
				
				
				//refresh_token 过期
				$dataTwo=$DyToools->renew_refresh_token($token['refresh_token']);
				if( isset($dataTwo['data']['error_code']) && $dataTwo['data']['error_code']==0 ){
					
					
					$infoToken->upRef($dataTwo['data'],$open_id);//更新refresh_token
					$this->getFansData($open_id,$cursor);
					
					
				}else{
					$infoToken->upTokenStatus($open_id);//授权过期需用户重新授权 更改授权状态 为 0
				}
			}
			
		}else{

        }
	}
	//关注数据
	function getFollowingData($open_id,$cursor){
		$userToken=new HandleVideo();
		$infoToken=new Douyin();
        $DyToools=new DyToools();
		$token=$userToken->userToken($open_id);
		
		$url=$this->url."/following/list/?open_id={$token['open_id']}&access_token={$token['access_token']}&cursor={$cursor}&count=20";
		$data=$this->getJson($url);
		if( isset($data['extra']['error_code']) && $data['extra']['error_code']==0 ){
			
			
			if( isset($data['data']['list']) && $data['data']['list'] ){
				//更新、添加用户关注数据
				$userToken->upDyUserFollowing($data['data']['list'],$open_id);
			}
			if( $data['data']['cursor']>0 && $data['data']['has_more'] ){
				$this->getFollowingData($open_id,$data['data']['cursor']);
			}
			
			
		}elseif( isset($data['extra']['error_code']) && $data['extra']['error_code']==10008 || $data['extra']['error_code']=2190008 ){
			
			
			// access_token 过期
			$dataOne=$DyToools->refresh_token($token['refresh_token']);
			if( isset($dataOne['data']['error_code']) && $dataOne['data']['error_code']==0 ){
				
				
				$infoToken->upToken($dataOne['data'],$open_id);//更新Token
				$this->getFollowingData($open_id,$cursor);
				
				
			}elseif( isset($dataOne['data']['error_code']) && $dataOne['data']['error_code']==10010 ){
				
				
				//refresh_token 过期
				$dataTwo=$DyToools->renew_refresh_token($token['refresh_token']);
				if( isset($dataTwo['data']['error_code']) && $dataTwo['data']['error_code']==0 ){
					
					
					$infoToken->upRef($dataTwo['data'],$open_id);//更新refresh_token
					$this->getFollowingData($open_id,$cursor);
					
					
				}else{
					$infoToken->upTokenStatus($open_id);//授权过期需用户重新授权 更改授权状态 为 0
				}
			}
			
		}
	}
	
	//获取意向客户----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function getEnterprise(){
		set_time_limit(0);
		$userTokenListTrue=new HandleVideo();
		$userTokenListTrue=$userTokenListTrue->userqyhTokenListTrue();
		//print_r($userTokenListTrue);exit();
		if($userTokenListTrue){
			foreach($userTokenListTrue as $k=>$v){
				// if($k==0){
					$this->getEnterpriseData($v['open_id'],1);
				// }
			}
		}
	}
	//  获取意向客户的方法
	function getEnterpriseData($open_id,$cursor){//
		$userToken=new HandleVideo();
		$infoToken=new Douyin();
        $DyToools=new DyToools();
		$token=$userToken->userToken($open_id);
	    $start_time=strtotime("-1 year -0 month -0 day");//int类型 时间戳吧 设定一年内有互动的都获取
        $end_time=time();//int类型 结束时间
//		$action_type=0;
	//	print_r($start_time);echo '111xx'; print_r($end_time); exit();
		$url=$this->url."/enterprise/leads/user/list/?open_id={$token['open_id']}&access_token={$token['access_token']}&cursor={$cursor}&count=20";
		$url.="&start_time=$start_time&end_time=$end_time&action_type=0";
//		$url.="&start_time=".strtotime(date('Y-m-d',time())-24*3600)."&end_time=".strtotime(date('Y-m-d',time())-1)."&action_type=0";
		$data=$this->getJson($url);
		//echo '<pre/>'; print_r($data);
		if( isset($data['data']['error_code']) && $data['data']['error_code']==0 ){
			if( isset($data['data']['users']) && $data['data']['users'] ){
				//更新、添加用户意向客户
				$userToken->upDyUserEnterprise($data['data']['users'],$open_id);
			}
			if( $data['data']['cursor']>0 && $data['data']['has_more'] ){
				$this->getEnterpriseData($open_id,$data['data']['cursor']);
			}

		}elseif( isset($data['data']['error_code']) && $data['data']['error_code']==10008 || $data['data']['error_code']=2190008 ){

			// access_token 过期
			$dataOne=$DyToools->refresh_token($token['refresh_token']);
			if( isset($dataOne['data']['error_code']) && $dataOne['data']['error_code']==0 ){
				$infoToken->upToken($dataOne['data'],$open_id);//更新Token
				$this->getEnterpriseData($open_id,$cursor);
				
				
			}elseif( isset($dataOne['data']['error_code']) && $dataOne['data']['error_code']==10010 ){
				
				
				//refresh_token 过期
				$dataTwo=$DyToools->renew_refresh_token($token['refresh_token']);
				if( isset($dataTwo['data']['error_code']) && $dataTwo['data']['error_code']==0 ){
					
					
					$infoToken->upRef($dataTwo['data'],$open_id);//更新refresh_token
					$this->getEnterpriseData($open_id,$cursor);
					
					
				}else{
					$infoToken->upTokenStatus($open_id);//授权过期需用户重新授权 更改授权状态 为 0
				}
			}
			
		}
	}

	//get请求
	function getJson($url){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
		curl_setopt($ch, CURLOPT_TIMEOUT,60);//连接后等待时间
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);//连接前等待时间
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_HEADER, FALSE);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);//为true，则会跟踪爬取重定向页面，否则，不会跟踪重定向页面
		curl_setopt($ch, CURLOPT_ENCODING, "");//返回乱码处理
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书下同
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		$output = curl_exec($ch);
		curl_close($ch);
		return json_decode($output, true);
	}
}